//
//  BCPayPasswordTextFeild.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/12/11.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "BCPayPasswordTextFeild.h"

static NSString *PayPasswordTextString = @"0123456789";

@protocol BCPayPasswordTextFeildInputViewDelegate <NSObject>

- (void)payPasswordTextFeildInputViewInputText:(NSString *)text;
- (void)payPasswordTextFeildInputViewDeleteAction;

@end

@interface BCPayPasswordTextFeildInputView : UIView

/**
 *numberButtons
 */
@property (nonatomic, strong) NSMutableArray *numberButtons;

/**
 *deleteButton
 */
@property (nonatomic, strong) QMUIButton *deleteButton;

/**
 * BCPayPasswordTextFeildInputViewDelegate
 */
@property (nonatomic, weak) id <BCPayPasswordTextFeildInputViewDelegate> delegate;

@end

@implementation BCPayPasswordTextFeildInputView

- (NSMutableArray *)numberButtons {
    if (!_numberButtons) {
        _numberButtons = [NSMutableArray arrayWithCapacity:10];
    }
    return _numberButtons;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:[self.class defaultBounds]];
    if (self) {
        [self setupButtons];
    }
    return self;
}


- (void)setupButtons {
    CGFloat padding = 8.0;
    CGSize regionSize = [self.class defaultSize];
    CGFloat buttonWidth = (regionSize.width-4*padding)/3.0;
    CGFloat buttonHeight = (regionSize.height-5*padding)/4.0;
    //
    [self.numberButtons removeAllObjects];
    NSInteger count = PayPasswordTextString.length;
    for (int i=0; i<count; i++) {
        QMUIButton *button = [QMUIButton new];
//        [button setTitle:@"0" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self addSubview:button];
        button.backgroundColor = [UIColor whiteColor];
        button.titleLabel.font = [UIFont systemFontOfSize:23 weight:UIFontWeightMedium];
        button.layer.cornerRadius = padding;
        //
        NSInteger line = i/3;
        NSInteger idx = i%3;
        CGFloat leftOffset = (padding+buttonWidth)*idx+padding;
        CGFloat topOffset = (padding+buttonHeight)*line+padding;
        if (i==count-1) {
            leftOffset = (padding+buttonWidth)*1+padding;
        }
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self).offset(leftOffset);
            make.top.mas_equalTo(self).offset(topOffset);
            make.width.mas_equalTo(buttonWidth);
            make.height.mas_equalTo(buttonHeight);
        }];
        //
        [self.numberButtons addObject:button];
        //
        [button addTarget:self action:@selector(buttonDidClickedAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    QMUIButton *lastButton = self.numberButtons.lastObject;
    //
    self.deleteButton = [QMUIButton new];
    [self.deleteButton setImage:LOAD_IMAGE(@"DDIDCardInputDeleteImageNormal") forState:UIControlStateNormal];
    [self.deleteButton setImage:LOAD_IMAGE(@"DDIDCardInputDeleteImageHighlighted") forState:UIControlStateHighlighted];
    self.deleteButton.layer.cornerRadius = lastButton.layer.cornerRadius;
    [self addSubview:self.deleteButton];
    [self.deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(lastButton.mas_right).offset(padding);
        make.top.mas_equalTo(lastButton);
        make.width.mas_equalTo(lastButton);
        make.height.mas_equalTo(lastButton);
    }];
    [self.deleteButton addTarget:self action:@selector(deleteButtonDidClickedAction) forControlEvents:UIControlEventTouchUpInside];
}

- (void)updateNumbers {
    NSString *string = PayPasswordTextString;
    NSInteger buttonCount = self.numberButtons.count;
    for (int i=0; i<buttonCount; i++) {
        QMUIButton *button = self.numberButtons[i];
        NSInteger length = string.length;
        NSInteger randomIdx = random()%length;
        NSString *title = [string substringWithRange:NSMakeRange(randomIdx, 1)];
        [button setTitle:title forState:UIControlStateNormal];
        string = [string stringByReplacingOccurrencesOfString:title withString:@""];
    }
}

- (void)buttonDidClickedAction:(QMUIButton *)button {
    NSString *text = [button titleForState:UIControlStateNormal];
    if (self.delegate && [self.delegate respondsToSelector:@selector(payPasswordTextFeildInputViewInputText:)]) {
        [self.delegate payPasswordTextFeildInputViewInputText:text];
    }
}
- (void)deleteButtonDidClickedAction {
    if (self.delegate && [self.delegate respondsToSelector:@selector(payPasswordTextFeildInputViewDeleteAction)]) {
        [self.delegate payPasswordTextFeildInputViewDeleteAction];
    }
}

+ (CGSize)defaultSize {
    return CGSizeMake(ScreenWidth, ScreenWidth*(446.0/750.0));
}
+ (CGRect)defaultBounds {
    CGSize size = [self defaultSize];
    return CGRectMake(0, 0, size.width, size.height+Height_Bottom_SafeArea);
}

@end

@interface BCPayPasswordTextFeild () <BCPayPasswordTextFeildInputViewDelegate>

@property (nonatomic, strong) BCPayPasswordTextFeildInputView *customInputView;

@end

@implementation BCPayPasswordTextFeild

- (BCPayPasswordTextFeildInputView *)customInputView {
    if (!_customInputView) {
        _customInputView = [[BCPayPasswordTextFeildInputView alloc] initWithFrame:CGRectZero];
        _customInputView.delegate = self;
    }
    return _customInputView;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.inputView = self.customInputView;
        //
        [self.customInputView updateNumbers];
        //
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldTextDidBeginEditingNotification) name:UITextFieldTextDidBeginEditingNotification object:self];
    }
    return self;
}

#pragma mark - BCPayPasswordTextFeildInputViewDelegate
- (void)payPasswordTextFeildInputViewInputText:(NSString *)text {
    if (text.length) {
        NSString *currentText = self.text;
        if (currentText.length<6) {
            self.text = [self.text stringByAppendingString:text];
            if (self.text.length==6) {
                if (self.completeDelegate&&[self.completeDelegate respondsToSelector:@selector(payPasswordTextFeildInputCompleteAction)]) {
                    [self.completeDelegate payPasswordTextFeildInputCompleteAction];
                }
            }
        }
    }
}

- (void)payPasswordTextFeildInputViewDeleteAction {
    [self deleteBackward];
}




- (void)textFieldTextDidBeginEditingNotification {
    //    [self.customInputView updateNumbers];
}



- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidBeginEditingNotification object:self];
}

- (void)deleteBackward {
    [super deleteBackward];
}

@end
